//
//  YCApplyWithDrawController.m
//  YCVillageDoctor
//
//  Created by zhangji on 2017/6/30.
//  Copyright © 2017年 zhangxiaoliang. All rights reserved.
//

#import "YCApplyWithDrawController.h"
#import "YCApplyCashViewController.h"
#import "YCAppLyCashDetailViewController.h"
#import "YCWithDrawHeadView.h"
#import "UIImage+Resizable.h"
#import "YCWithDrawTableViewCell.h"
#import "YCWithDrawModel.h"

@interface YCApplyWithDrawController ()

@property (strong, nonatomic) YCWithDrawModel * withDrawModel;

@property (strong, nonatomic) NSArray * imageNameArray;

@property (strong, nonatomic) NSArray * titleNameArray;

@property (strong, nonatomic) NSArray * moneyArray;

@property (nonatomic, assign) CGFloat totalMoney;


@end


static NSString * const Kidentifier = @"Kidentifier";

@implementation YCApplyWithDrawController


#pragma mark - Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    [self setUpUI];
    
    [self getData];
    
    
}

- (void)viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];
    
    self.navigationController.navigationBarHidden = NO;

}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = YES;

}

#pragma mark - Lazy Load

- (NSArray *)imageNameArray {

    if (!_imageNameArray) {
        
        _imageNameArray = @[@"heart_img",
                            @"lung_img",
                            @"liver_img",
                            @"eye_img",
                            @"gym_img"];
    }
    
    return _imageNameArray;

}

- (NSArray *)titleNameArray {

    if (!_titleNameArray) {
        
        _titleNameArray = @[@"百倍爱心卡",
                            @"肺保卡",
                            @"肝保卡",
                            @"百倍眼保卡",
                            @"女性无忧卡"];
        
    }
    
    return _titleNameArray;
}


#pragma mark - getDate

- (void)getData {
    
    NSString * url = WithDrawUrl;
    
    NSDictionary * param = @{@"link_mobile":judgeString([YCUserTool sharedTools].linkMobile)};
    
    [self presentLoadingTips:nil];
    
    self.totalMoney = 0.00;
    [DownloadData post:url params:param success:^(NSMutableDictionary * json) {
        
        
        if (json) {
            
            [self dismissTips];
            self.withDrawModel = [YCWithDrawModel mj_objectWithKeyValues:json];
            
            for (int i = 0 ; i < json.allValues.count; i ++) {
                
                self.totalMoney += [json.allValues[i] floatValue];
                [self.tableView reloadData];
            }
            
            
        }
        
    } failure:^(NSError *error) {
        
        [self dismissTips];
        
        
    }];


}

#pragma mark - CreateUI

- (void)setUpUI {
    
    self.navigationController.navigationBarHidden = YES;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.title = @"申请提现";
    
    [self.tableView registerClass:[YCWithDrawTableViewCell class] forCellReuseIdentifier:Kidentifier];
    
    self.tableView.sectionFooterHeight = 5;
    UIView * footerView = [UIView new];
    footerView.backgroundColor = [UIColor lightTextColor];
    self.tableView.tableFooterView = footerView;
    UIButton * clickBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [clickBtn setTitle:@"想要提现? 先查看提现步骤" forState:UIControlStateNormal];
    clickBtn.titleLabel.font = [UIFont systemFontOfSize:13.0];
    clickBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [clickBtn addTarget:self action:@selector(push) forControlEvents:UIControlEventTouchUpInside];
    [clickBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.view addSubview:clickBtn];
    
    [clickBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(self.view.mas_top).offset(SCREEN_HEIGHT - 30);
        make.size.mas_equalTo(CGSizeMake(200, 20));
    }];

}



- (instancetype)initWithStyle:(UITableViewStyle)style {

    return [super initWithStyle:UITableViewStyleGrouped];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.imageNameArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YCWithDrawTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Kidentifier forIndexPath:indexPath];
    
    if (!cell) {
        
        cell = [[YCWithDrawTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Kidentifier];
    }
    
    
    switch (indexPath.row) {
        case 0:

            cell.moneyLabel.text = [NSString stringWithFormat:@"%.2f元",[self.withDrawModel.heart_money floatValue]];
            
            break;
        case 1:
            
            cell.moneyLabel.text = [NSString stringWithFormat:@"%.2f元",[self.withDrawModel.tum_money floatValue]];

            break;
        case 2:
            
            cell.moneyLabel.text = @"0.00元";
            
            break;
        case 3:
            
            cell.moneyLabel.text = [NSString stringWithFormat:@"%.2f元",[self.withDrawModel.eye_money floatValue]];
            break;
        case 4:
            
            cell.moneyLabel.text = [NSString stringWithFormat:@"%.2f元",[self.withDrawModel.gyn_money floatValue]];
            break;
            
            

        default:
            break;
    }
    // Configure the cell...
    
    cell.imgView.image = [UIImage imageNamed:self.imageNameArray[indexPath.row]];
    cell.nameLabel.text = self.titleNameArray[indexPath.row];
    
    
    return cell;
}

- (void)push {

    YCAppLyCashDetailViewController *applyCashVc = [[YCAppLyCashDetailViewController alloc]init];
    [self.navigationController pushViewController:applyCashVc animated:YES];

}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    YCWithDrawHeadView * v = [[YCWithDrawHeadView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 180 + NAVGATION_ADD_STATUSBAR_HEIGHT)];
    v.moneyString = [NSString stringWithFormat:@"%.2f",self.totalMoney];
    return v;

}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return 180 + NAVGATION_ADD_STATUSBAR_HEIGHT;

}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
